package com.dhj.streamTest2;

import com.dhj.streamTest.entity.Author;
import com.dhj.streamTest.entity.Book;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.dhj.streamTest.entity.AuthorList.getAuthors;

public class Demo4 {

    public static void main(String[] args) {
        List<Author> authors = getAuthors();

        List<String> collect = authors.stream().distinct()
                .map(author -> author.getName())
                .collect(Collectors.toList());

        System.out.println(collect);


        System.out.println("------------------------");


        Set<String> collect1 = authors.stream().distinct()
                .flatMap(author -> author.getBooks().stream())
                .map(book -> book.getName())
                .collect(Collectors.toSet());

        System.out.println(collect1);


        System.out.println("----------------------------");


        Map<String, List<Book>> collect2 = authors.stream().distinct()
                .collect(Collectors.toMap(new Function<Author, String>() {
                    @Override
                    public String apply(Author author) {
                        return author.getName();
                    }
                }, new Function<Author, List<Book>>() {
                    @Override
                    public List<Book> apply(Author author) {
                        return author.getBooks();
                    }
                }));

        System.out.println(collect2);


        System.out.println("----------------------------");


        Map<String, List<String>> collect3 = authors.stream().distinct()
                .collect(Collectors.toMap(new Function<Author, String>() {
                    @Override
                    public String apply(Author author) {
                        return author.getName();
                    }
                }, new Function<Author, List<String>>() {
                    @Override
                    public List<String> apply(Author author) {
                         return author.getBooks().stream().map(book -> book.getName()).collect(Collectors.toList());
                    }
                }));

        System.out.println(collect3);


        System.out.println("----------------------------");


        Map<String, List<String>> collect4 = authors.stream().distinct()
                .collect(Collectors.toMap(author -> author.getName(), author -> author.getBooks().stream().map(book -> book.getName()).collect(Collectors.toList())));

        System.out.println(collect4);
    }
}
